package com.example.wumeng.spinner;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class FourActivity extends AppCompatActivity {

    private Spinner spinner1;
    private Spinner spinner2;
    private TextView textView;
    private String string1;

    private String[][] dataStrings = {{"苹果","香蕉","梨子","菠萝","草莓",},{"教师","工人","程序员",},
            {"老虎", "狮子", "大象",}};

    private ArrayAdapter adapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_four);

        spinner1 = findViewById(R.id.spinner3);
        spinner2 = findViewById(R.id.spinner4);
        textView = findViewById(R.id.text3);

        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                adapter = new ArrayAdapter<CharSequence>(FourActivity.this, android.R.layout.simple_spinner_item,dataStrings[position]);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinner2.setAdapter(adapter);
                // parent指的是spinner1
                string1 = parent.getItemAtPosition(position).toString(); // 获取当前文本
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String string2 = parent.getItemAtPosition(position).toString();
                textView.setText(string1 + ":" + string2);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }
}
